﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;


namespace MdcService.Static
{
	public static class Utilities
	{
		public static DateTime FromUnixTime(this long unixTime)
		{
			var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
			return epoch.AddSeconds(unixTime);
		}

		public static long ToUnixTime1(this DateTime date)
		{
			return (date.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
		}

		public static string NewId()
		{
			return $"Machine_{Guid.NewGuid().ToString("N")}";
		}

		public static string SerializeObject(object o)
		{
			string json = JsonConvert.SerializeObject(o);
			return json;
		}

		public static T DeserializeJsonToObject<T>(string json) where T : class
		{
			JsonSerializer serializer = new JsonSerializer();
			StringReader sr = new StringReader(json);
			object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
			T t = o as T;
			return t;
		}

		public static List<T> DeserializeJsonToList<T>(string json) where T : class
		{
			JsonSerializer serializer = new JsonSerializer();
			StringReader sr = new StringReader(json);
			object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
			List<T> list = o as List<T>;
			return list;
		}

		public static T DeserializeAnonymousType<T>(string json, T anonymousTypeObject)
		{
			T t = JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject);
			return t;
		}

	    public static int Last = 100;
	    public static int GetRandomInt(int max = 10000)
	    {

	        Random r = new Random(Last);
	        Last = r.Next(max);
	        return Last;
	    }

	    public static long Uid = 0;

	    public static long GetNewId()
	    {
	        Uid++;
	        return Uid;
	    }

	    public static void TestingDelay(int t = 1000)
	    {
	        Thread.Sleep(t);
	    }
    }
}
